package com.health.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.system.entity.Role;
import com.health.system.mapper.RoleMapper;
import com.health.system.service.RoleService;
import com.health.system.vo.AdminRoleVo;
import com.health.system.vo.RoleListPageSearchVo;
import com.health.system.vo.UpdateStatusVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public IPage<Role> selectListPage(Long current, Long size, RoleListPageSearchVo condition) {
        IPage<Role> page = new Page<>(current, size);
        IPage<Role> result = roleMapper.selectPage(page, new LambdaQueryWrapper<Role>()
                .like(StringUtils.isNotBlank(condition.getKeyword()), Role::getRoleName, condition.getKeyword()));

        result.getRecords().forEach(r -> {
            r.setMenuIds(roleMapper.selectMenuIdsById(r.getId()));
            r.setResourceIds(roleMapper.selectResourceIdsById(r.getId()));
        });

        return result;
    }

    @Transactional
    @Override
    public void insert(AdminRoleVo vo) {
        Role role = Role.builder()
                .roleName(vo.getName())
                .label(vo.getLabel())
                .build();

        roleMapper.insert(role);
        vo.getIds().forEach(mid -> roleMapper.insertRelationMenu(role.getId(), mid));
    }

    @Transactional
    @Override
    public void allocMenu(AdminRoleVo vo) {
        Role role = Role.builder()
                .id(vo.getId())
                .roleName(vo.getName())
                .label(vo.getLabel())
                .build();
        roleMapper.updateById(role);

        vo.getIds().forEach(mid -> roleMapper.deleteRelationMenu(vo.getId()));
        vo.getIds().forEach(mid -> roleMapper.insertRelationMenu(vo.getId(), mid));
    }

    @Transactional
    @Override
    public void allocResource(AdminRoleVo vo) {
        Role role = Role.builder()
                .id(vo.getId())
                .roleName(vo.getName())
                .label(vo.getLabel())
                .build();
        roleMapper.updateById(role);

        vo.getIds().forEach(rid -> roleMapper.deleteRelationResource(vo.getId()));
        vo.getIds().forEach(rid -> roleMapper.insertRelationResource(vo.getId(), rid));
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        ids.forEach(id -> {
            roleMapper.deleteRelationMenu(id);
            roleMapper.deleteRelationResource(id);
            roleMapper.deleteById(id);
        });
    }

    @Override
    public void updateStatus(UpdateStatusVo vo) {
        Role role = Role.builder()
                .id(vo.getId())
                .isEnable(vo.getStatus())
                .build();
        roleMapper.updateById(role);
    }
}
